import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const storeKey = {
		nwStateChange: 'networkStateChange',
		// 刷新token
		refreshToken: 'lzj_refreshToken',
		//用户信息
		userInfo: 'lzj_userInfo',
		//请求凭证
		loginToken: 'lzj_token',
		
		
		
	
	};

const store = new Vuex.Store({
	state: {
		networkState: 'unknown',
		loginToken: uni.getStorageSync(storeKey.loginToken) || '', //登录token
		hasLogin: false,
		userInfo: uni.getStorageSync(storeKey.userInfo) || {},
		currAudioId: 'none',
		//是否授权过用户信息
		isAuthUser: '',
		//授权的用户信息
		authUserInfo: uni.getStorageSync('lzj_authUserInfo') || '',
		miniappcode: uni.getStorageSync('lzj_miniappcode') || '',//授权码
	},
	getters: {
		// 获取网络状态
		networkStatus: state => {
			return state.networkState;
		},
	    // 判断用户是否登录
	    hasLogin: state => {
	        // console.log('是否登录！！',state.loginToken)
			if(state.loginToken)
			{
				return true;
			}else{
				return false;
			}
	    },
		
		
		getLoginToken: state => {
			return state.loginToken;
		}
	},
	mutations: {
		login(state, provider) {
			let userInfo = provider,
				loginToken = userInfo.token;
				
			state.userInfo = userInfo;
			state.loginToken =loginToken;
			uni.setStorageSync(storeKey.loginToken, loginToken);
			uni.setStorageSync(storeKey.userInfo, userInfo);
			
		},
		logout(state) {
			state.loginToken = '';
			state.userInfo = {};
			uni.removeStorageSync(storeKey.loginToken);
			uni.removeStorageSync(storeKey.userInfo);
			
			state.isAuthUser = '';
			state.authUserInfo = '';
			state.miniappcode = '';
			uni.removeStorageSync('lzj_authUserInfo');
			uni.removeStorageSync('lzj_miniappcode')
			// uni.clearStorageSync();
		},
		/* 更新用户信息 */
		updateUserInfo(state, userInfo){
			state.userInfo = userInfo
			uni.setStorageSync(storeKey.userInfo, userInfo);
		},
		
		
		/**
		 * 用户授权用户信息状态更改
		 */
		updateAuthUser(state,provider){
			state.authUserInfo = provider;
			uni.setStorageSync('lzj_authUserInfo',provider);
		},
		removeAuthUser(state){
			state.authUserInfo = '';
			uni.removeStorageSync('lzj_authUserInfo')
		},
		
		/**
		 * 更新授权码
		 * @param {Object} state
		 * @param {Object} provider
		 */
		updateMiniappcode(state,provider){
			state.miniappcode = provider;
			uni.setStorageSync('lzj_miniappcode',provider);
		},
		removeMiniappcode(state){
			state.miniappcode = '';
			uni.removeStorageSync('lzj_miniappcode')
		},
		
		
		/* login(state, provider) {
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',  
			    data: provider  
			}) 
			console.log(state.userInfo);
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorage({  
                key: 'userInfo'  
            })
		}, */
		setReaderId(state, readerId){
			state.userInfo.readerId = readerId;
			uni.setStorageSync(storeKey.userInfo, state.userInfo);
		},
		clearReaderId(state){
			state.userInfo.readerId = null;
			uni.setStorageSync(storeKey.userInfo, state.userInfo);
		},
		setUserImg(state, userImg){
			state.userInfo.userImg = userImg;
			uni.setStorageSync(storeKey.userInfo, state.userInfo);
			
		},
		setScore(state, score){
			state.userInfo.score = score;
			uni.setStorageSync(storeKey.userInfo, state.userInfo);
		},
		setUserInfo(state, userInfo){
			state.userInfo = userInfo;
			uni.setStorageSync(storeKey.userInfo, state.userInfo);
		},
		setCurrAudioId(state,payload){
			state.currAudioId=payload;
		},
	},
	actions: {
	
	}
})

export default store
